//
//  LoginViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/10.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "LoginViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "LoginViewModel.h"
#import <MBProgressHUD.h>
#import <AFNetworking.h> 
#import "NetErrorStateCode.h"
#import "ForgetPassWordViewController.h"
@interface LoginViewController (){

    LoginViewModel *_loginViewModel;
    
}
@property (nonatomic ,weak) IBOutlet UITextField *usernameTextField;
@property (nonatomic ,weak) IBOutlet UITextField *passwordTextField;

@property (nonatomic ,weak) IBOutlet UIButton *loginButton;
@property (nonatomic ,weak) IBOutlet UIButton *forgetPasswd;

- (IBAction)dismissSelf:(id)sender;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _loginButton.layer.cornerRadius = 5.0f;
    _loginButton.layer.masksToBounds = YES;
    
    RAC(_loginButton,backgroundColor) = [RACSignal combineLatest:@[_usernameTextField.rac_textSignal,_passwordTextField.rac_textSignal] reduce:^id(NSString *username,NSString *password){
        return [username length]>0 && [password length]>0?[UIColor redColor]:[UIColor grayColor];
    }];
    
    RAC(_loginButton,enabled) = [RACSignal combineLatest:@[_usernameTextField.rac_textSignal,_passwordTextField.rac_textSignal] reduce:^id(NSString *username,NSString *password){
        return @([username length]>0 && [password length]>0);
        
        
    }];
    
    @weakify(self);
    
    RAC(_forgetPasswd,enabled) = [RACSignal combineLatest:@[_usernameTextField.rac_textSignal] reduce:^id(NSString *username){
        return @([username length]==11 && [username hasPrefix:@"1"]);
    }];
    RAC(_forgetPasswd,hidden) = [RACSignal combineLatest:@[_usernameTextField.rac_textSignal] reduce:^id(NSString *username){
        return @(!([username length]==11 && [username hasPrefix:@"1"]));
    }];
    
    [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        hud.mode = MBProgressHUDModeDeterminate;
        hud.labelText = @"正在登录";
        [hud show:YES];
        [self.view addSubview:hud];
        self.view.alpha = .5;
        [[[LoginViewModel loginWithUsername:_usernameTextField.text PassWord:_passwordTextField.text] deliverOnMainThread] subscribeNext:^(NSDictionary *responseObject) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
            
            [hud hide:YES];
            self.view.alpha =1;
            [self dismissSelf:nil];
            
        
        } error:^(NSError *error) {
            NSLog(@"xxx%@",[error localizedDescription]);
            
            hud.labelText = [NetErrorStateCode getDescribeText:error];
            
            
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(1.5);
            } completionBlock:^{
                self.view.alpha = 1;
            }];
            
            
            
            
        } completed:^{
        
        }];
    
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dismissSelf:(id)sender{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
   
    
}



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 
//     Get the new view controller using [segue destinationViewController].
//     Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"forget"]) {
        ForgetPassWordViewController *destinationViewController = [segue destinationViewController];
        destinationViewController.phoneString = _usernameTextField.text;
        
    }
    
}


@end
